{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: NumericalConcept Extraction with References and Justifications"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: NumericalConcept Extraction with References and Justifications\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Document, DocumentLLM, NumericalConcept\n",
        "\n",
        "\n",
        "# Document with values that require calculation/inference\n",
        "report_text = \"\"\"\n",
        "Quarterly Sales Report - Q2 2023\n",
        "\n",
        "Product A: Sold 450 units at $75 each\n",
        "Product B: Sold 320 units at $125 each\n",
        "Product C: Sold 180 units at $95 each\n",
        "\n",
        "Marketing expenses: $28,500\n",
        "Operating costs: $42,700\n",
        "\"\"\"\n",
        "\n",
        "# Create a Document from the text\n",
        "doc = Document(raw_text=report_text)\n",
        "\n",
        "# Create a NumericalConcept for total revenue\n",
        "total_revenue_concept = NumericalConcept(\n",
        "    name=\"Total quarterly revenue\",\n",
        "    description=\"The total revenue calculated by multiplying units sold by their price\",\n",
        "    add_justifications=True,\n",
        "    justification_depth=\"comprehensive\",  # Detailed justification to show calculation steps\n",
        "    justification_max_sents=4,  # Maximum number of sentences for justification\n",
        "    add_references=True,\n",
        "    reference_depth=\"paragraphs\",  # Reference specific paragraphs\n",
        "    singular_occurrence=True,  # Ensure that the data is merged into a single item\n",
        ")\n",
        "\n",
        "# Attach the concept to the document\n",
        "doc.add_concepts([total_revenue_concept])\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/o4-mini\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract the concept\n",
        "total_revenue_concept = llm.extract_concepts_from_document(doc)[0]\n",
        "\n",
        "# Print the extracted inferred value with justification\n",
        "print(\"Calculated total quarterly revenue:\")\n",
        "for item in total_revenue_concept.extracted_items:\n",
        "    print(f\"\\nTotal Revenue: {item.value}\")\n",
        "    print(f\"Calculation Justification: {item.justification}\")\n",
        "    print(\"Source references:\")\n",
        "    for para in item.reference_paragraphs:\n",
        "        print(f\"- {para.raw_text}\")\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}